package com.example.springboottestgit.controller.main;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.example.springboottestgit.bean.InstructData;
import com.example.springboottestgit.utils.ExcelExportUtil;
import com.example.springboottestgit.utils.FileUtil;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;

/**
 * @description:
 * @author: ZZZ
 */
public class AirQualityCSVReader {
    public static void main(String[] args) {
        read();
    }

    public static void read() {

        List<AirQuality> listAll = new ArrayList<>();

        List<AirQuality> yzj = new ArrayList<>();

        List<AirQuality> list1 = new ArrayList<>();

        List<AirQuality> list5 = new ArrayList<>();


        int n = 0;

        Map<String, AirQuality> map1 = new HashMap<>();

        Map<Integer, AirQuality> map5 = new HashMap<>();

        String csvFile = "C:\\Users\\user\\Desktop\\beijing.csv";
        String line = "";
        String encoding = "GBK";
        String csvSplitBy = ",";
        try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(csvFile), encoding))) {
            //try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
            while ((line = br.readLine()) != null) {
                // 使用逗号分割每一行
                String[] columns = line.split(csvSplitBy);
                String date = columns[0];
                if (date.length() < 4) {
                    continue;
                }
                String level = columns[1];
                String AQI = columns[2];
                String PM2 = columns[4];
                String PM10 = columns[5];
                String SO2 = columns[6];
                String NO2 = columns[7];
                String CO = columns[8];
                String O3 = columns[9];

                AirQuality air = new AirQuality();
                air.setDate(date);
                air.setLevel(level);
                air.setAqi(AQI);
                air.setPm2(PM2);
                air.setPm10(PM10);
                air.setSo2(SO2);
                air.setNo2(NO2);
                air.setCo(CO);
                air.setO3(O3);

                InstructData instructData = new InstructData();

                String instruction = "请根据我提供的空气质量中污染物浓度的含量，帮我预测空气的质量等级和AQI指数。";
                String input = String.format("PM2.5 : %s, PM10 : %s, So2 : %s, No2 : %s, Co : %s, O3 : %s", PM2, PM10, SO2, NO2, CO, O3);
                String output = String.format("质量等级：%s，AQI指数：%s", level, AQI);
                instructData.setInstruction(instruction);
                instructData.setInput(input);
                instructData.setOutput(output);

                n++;

                if (n > 3200) {
                    yzj.add(air);
                } else {
                    listAll.add(air);
                    map1.put(date, air);
                    String str = String.format("质量等级：%s，AQI指数：%s,PM2.5 : %s, PM10 : %s, So2 : %s, No2 : %s, Co : %s, O3 : %s;", level, AQI, PM2, PM10, SO2, NO2, CO, O3);
                    map5.put(n, air);
                }


                // 输出每一列的内容
//                for (String column : columns) {
//                    System.out.print(column + "\t");
//                }
//                System.out.println();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        //String result = JSONObject.toJSONString(list);
        //FileUtil.txtWriter("C:\\Users\\user\\Desktop\\数据集3200.json", result);

        List<String> listRandom1 = new ArrayList<>();
        export(yzj, "C:\\Users\\user\\Desktop\\北京空气质量\\验证集A-连续.xlsx");
        yzj.clear();
        for (int i = 2013; i <= 2024; i++) {
            for (int j = 1; j <= 12; j++) {
                String d1 = i + "/" + j + "/" + (new Random().nextInt(10) + 1);
                String d2 = i + "/" + j + "/" + (new Random().nextInt(10) + 11);
                String d3 = i + "/" + j + "/" + (new Random().nextInt(10) + 21);
                if (map1.containsKey(d1)) {
                    yzj.add(map1.get(d1));
                    listRandom1.add(d1);
                }

                if (map1.containsKey(d2)) {
                    yzj.add(map1.get(d2));
                    listRandom1.add(d2);
                }

                if (map1.containsKey(d3)) {
                    yzj.add(map1.get(d3));
                    listRandom1.add(d2);
                }
            }
        }
        export(yzj, "C:\\Users\\user\\Desktop\\北京空气质量\\验证集B-随机.xlsx");

        for (AirQuality airQuality : listAll) {
            if (airQuality.getDate().length() < 5) {
                continue;
            }

            if (!listRandom1.contains(airQuality.getDate())) {
                list1.add(airQuality);
            }
        }

        export(list1, "C:\\Users\\user\\Desktop\\北京空气质量\\数据集3200-不包含验证集.xlsx");

        List<InstructData> zl1 = new ArrayList<>();
        for (AirQuality airQuality : list1) {
            String level = airQuality.getLevel();
            String AQI = airQuality.getAqi();
            String PM2 = airQuality.getPm2();
            String PM10 = airQuality.getPm10();
            String SO2 = airQuality.getSo2();
            String NO2 = airQuality.getNo2();
            String CO = airQuality.getCo();
            String O3 = airQuality.getO3();
            InstructData instructData = new InstructData();
            String instruction = "请根据我提供的空气质量中污染物浓度的含量，帮我预测空气的质量等级和AQI指数。";
            String input = String.format("PM2.5 : %s, PM10 : %s, So2 : %s, No2 : %s, Co : %s, O3 : %s", PM2, PM10, SO2, NO2, CO, O3);
            String output = String.format("质量等级：%s，AQI指数：%s", level, AQI);
            instructData.setInstruction(instruction);
            instructData.setInput(input);
            instructData.setOutput(output);
            zl1.add(instructData);
        }


        FileUtil.txtWriter("C:\\Users\\user\\Desktop\\北京空气质量\\数据集3200-不包含验证集.json", listPretty(zl1));
        List<Integer> randomZl5 = new ArrayList<>();
        List<InstructData> zl5 = new ArrayList<>();
        for (int i = 0; i < 600; i++) {
            int start = new Random().nextInt(n) + 1;
            if (randomZl5.contains(start)) {
                continue;
            }
            randomZl5.add(start);
            int end = start + 5;
            if (map5.containsKey(end)) {
                InstructData instructData = new InstructData();
                String instruction = "请根据提供的过去5天空气质量情况，帮我预测下一天的空气质量情况。";
                String input = getStr(map5.get(start)) + "\n" + getStr(map5.get(start + 1)) + "\n" + getStr(map5.get(start + 2)) + "\n" + getStr(map5.get(start + 3)) + "\n" + getStr(map5.get(start + 4));
                String output = getStr(map5.get(start + 5));
                instructData.setInstruction(instruction);
                instructData.setInput(input);
                instructData.setOutput(output);
                zl5.add(instructData);

                list5.add(map5.get(start));
                list5.add(map5.get(start + 1));
                list5.add(map5.get(start + 2));
                list5.add(map5.get(start + 3));
                list5.add(map5.get(start + 4));
                list5.add(map5.get(start + 5));

            }
        }

        export(list5, "C:\\Users\\user\\Desktop\\北京空气质量\\数据集3200-随机抽取连续5条.xlsx");
        FileUtil.txtWriter("C:\\Users\\user\\Desktop\\北京空气质量\\数据集3200-随机抽取连续5条.json", listPretty(zl5));


        List<InstructData> oneAir5 = new ArrayList<>();
        List<AirQuality> oneAirList = new ArrayList<>();
        List<Integer> test = new ArrayList<>();
        for (int i = 0; i < 600; i++) {
            int start = new Random().nextInt(n) + 1;
            if (test.contains(start)) {
                continue;
            }
            test.add(start);
            int end = start + 5;
            if (map5.containsKey(end)) {
                String pm2 = "";
                String pm10 = "";
                String so2 = "";
                String no2 = "";
                String co = "";
                String o3 = "";


                for (int j = start; j < end; j++) {
                    pm2 = pm2 + "PM2.5 : " + map5.get(j).getPm2() + ";\n";
                    pm10 = pm10 + "PM10 : " + map5.get(j).getPm10() + ";\n";
                    so2 = so2 + "So2 : " + map5.get(j).getSo2() + ";\n";
                    no2 = no2 + "No2 : " + map5.get(j).getNo2() + ";\n";
                    co = co + "Co : " + map5.get(j).getCo() + ";\n";
                    o3 = o3 + "O3 : " + map5.get(j).getO3() + ";\n";
                }

                generateInstructData(oneAir5, "请根据提供的过去5天空气中PM2.5污染物的浓度数据，帮我预测下一天的空气中PM2.5污染物的浓度数据。", pm2, "PM2.5 : " + map5.get(end).getPm2());
                generateInstructData(oneAir5, "请根据提供的过去5天空气中PM10污染物的浓度数据，帮我预测下一天的空气中PM10污染物的浓度数据。", pm10, "PM10 : " + map5.get(end).getPm10());
                generateInstructData(oneAir5, "请根据提供的过去5天空气中So2污染物的浓度数据，帮我预测下一天的空气中So2污染物的浓度数据。", so2, "So2 : " + map5.get(end).getSo2());
                generateInstructData(oneAir5, "请根据提供的过去5天空气中No2污染物的浓度数据，帮我预测下一天的空气中No2污染物的浓度数据。", no2, "No2 : " + map5.get(end).getNo2());
                generateInstructData(oneAir5, "请根据提供的过去5天空气中Co污染物的浓度数据，帮我预测下一天的空气中Co污染物的浓度数据。", co, "Co : " + map5.get(end).getCo());
                generateInstructData(oneAir5, "请根据提供的过去5天空气中O3污染物的浓度数据，帮我预测下一天的空气中O3污染物的浓度数据。", o3, "O3 : " + map5.get(end).getO3());


                oneAirList.add(map5.get(start));
                oneAirList.add(map5.get(start + 1));
                oneAirList.add(map5.get(start + 2));
                oneAirList.add(map5.get(start + 3));
                oneAirList.add(map5.get(start + 4));
                oneAirList.add(map5.get(start + 5));

            }
        }
        export(oneAirList, "C:\\Users\\user\\Desktop\\北京空气质量\\数据集3200-单气体随机抽取连续5条.xlsx");
        FileUtil.txtWriter("C:\\Users\\user\\Desktop\\北京空气质量\\数据集3200-单气体随机抽取连续5条.json", listPretty(oneAir5));


    }

    public static void generateInstructData(List<InstructData> oneAir5, String instruction, String input, String output) {
        InstructData instructData = new InstructData();
        instructData.setInstruction(instruction);
        instructData.setInput(input);
        instructData.setOutput(output);
        oneAir5.add(instructData);
    }

    public static String getStr(AirQuality airQuality) {
        String level = airQuality.getLevel();
        String AQI = airQuality.getAqi();
        String PM2 = airQuality.getPm2();
        String PM10 = airQuality.getPm10();
        String SO2 = airQuality.getSo2();
        String NO2 = airQuality.getNo2();
        String CO = airQuality.getCo();
        String O3 = airQuality.getO3();
        String str = String.format("质量等级：%s，AQI指数：%s,PM2.5 : %s, PM10 : %s, So2 : %s, No2 : %s, Co : %s, O3 : %s;", level, AQI, PM2, PM10, SO2, NO2, CO, O3);
        return str;

    }

    public static void export(List<AirQuality> result, String outputFilePath) {

        List<Map<String, Object>> list = JSONObject.parseObject(JSONObject.toJSONString(result), List.class);
        //表头数组 日期	质量等级	AQI指数	当天AQI排名	PM2.5	PM10	So2	No2	Co	O3
        String[] lables = new String[]{"日期", "质量等级", "AQI指数", "PM2.5", "PM10", "So2", "No2", "Co", "O3"};
        //查询数据对应的属性数组

        String[] fields = new String[]{"date", "level", "aqi", "pm2", "pm10", "so2", "no2", "co", "o3"};
        String title = "空气质量数据.xlsx";
        String headTitle = "指令集";
        //if (result.size() > 1) {
        //title = result.get(0).getName() + "(" + result.get(0).getCode() + ")" + "历史数据.xlsx";
        //headTitle = result.get(0).getName() + "(" + result.get(0).getCode() + ")" + "历史数据";
        //}
        ExcelExportUtil.export(outputFilePath, list, lables, fields, title);

    }

    public static String listPretty(List<InstructData> list) {
        String pretty = JSON.toJSONString(list,
                SerializerFeature.PrettyFormat,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteDateUseDateFormat);
        return pretty;
    }
}
